<?php // checklabs.php -- Checks open labs and available software

// Include important constants and set admin_home (we need to set the variable since file() doesn't interpret constants in pathnames)

require_once("init.php");
$admin_home = ADMIN_HOME;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
	<title>Lab Search</title>
	<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
	
	<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
	<meta name="author" content="Dylan J. Sather" />
	<meta name="revised" content= <?php

	/* This script checks the date_modified file to give the date at which a file on the 
	   map was last modified, thus giving us the date at which the map was last revised */

	$date_file = "$admin_home/map_files/checklabs_date_modified";
	$fh = fopen($date_file, 'r');
	$date = fread($fh, filesize($date_file));
	$date = trim($date);
	fclose($fh);
	print "\"Dylan J. Sather, $date\" />";

	?>

	<meta name="description" content="Grinnell College Campus Lab Map" />
	<meta name="keywords"
		content="Grinnell College, TC Corps, Technology Consultant,
			Computer lab, Map, Dylan J. Sather" />
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<meta http-equiv="Content-Script-Type" content="text/javascript" />
	<link rel="stylesheet" type="text/css" href="css/labmap.css" />
      </head>	   

<body>

<div>

<?php

if (isset($_GET['day'])) {
	$post_day = "{$_GET['day']}";
	$post_hour = "{$_GET['hour']}";
	}
else {
	$post_day = "{$_POST['post_day']}";
	$post_hour = "{$_POST['post_hour']}";
	}

if (($post_day == 'Monday' && $post_hour >= 9 && $post_hour < 18) ||
    ($post_day == 'Tuesday' && $post_hour >= 8 && $post_hour < 18) ||
    ($post_day == 'Wednesday' && $post_hour >= 8 && $post_hour < 18) ||
    ($post_day == 'Thursday' && $post_hour >= 8 && $post_hour < 18) ||
    ($post_day == 'Friday' && $post_hour >= 9 && $post_hour < 16) ||
    // The Helpdesk is closed on Saturdays
    ($post_day == 'Sunday' && $post_hour >= 12 && $post_hour < 18))	
	{  
	print '	
	<p class="obl">The Helpdesk is open <br />';
	}
else
	{
	print '
	<p class="obl">The Helpdesk is closed <br />';
	}

// GENCOMP -- I think I've got the hours right. Should do some test cases to find out...

if ((($post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Wednesday' ||
      $post_day == 'Thursday') && $post_hour >= 8 && $post_hour <= 23) ||
    (($post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Thursday' || $post_day == 'Friday') && $post_hour >= 0 && $post_hour < 2) || 
     ($post_day == 'Friday' && $post_hour >=8 && $post_hour < 17) ||
     ($post_day == 'Sunday' && $post_hour >=12 && $post_hour <= 23))
	{
	print '
	Gencomp is open <br />';
	}

else
	{
	print '
	Gencomp is closed <br />';
	}

// AV CENTER

if ((($post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Wednesday' ||
      $post_day == 'Thursday') && $post_hour >= 8 && $post_hour < 23) ||
     ($post_day == 'Friday' && $post_hour >=8 && $post_hour < 17) ||
     ($post_day == 'Saturday' && $post_hour >=10 && $post_hour < 18) ||
     ($post_day == 'Sunday' && $post_hour >=12 && $post_hour < 22))
        {
	print '
	The AV Center is open <br />';
	}
else
	{
	print '
	The AV Center is closed <br />';
	}

// Creative Computing Lab

if ((($post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Wednesday' || $post_day == 'Thursday') && ($post_hour >= 8 && $post_hour <= 23)) || ($post_day == 'Sunday' && $post_hour >= 12 && $post_hour <= 23))
	{
	print '
	The CCL is open <br />';
	}
else
	{
	print '
	The CCL is closed <br />';
	}

// Science Lab

if ((($post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Wednesday' || $post_day == 'Thursday') && ($post_hour >= 16 && $post_hour <= 23)) ||
      ($post_day == 'Sunday' && $post_hour >= 12 && $post_hour <= 23))
	{
	print '
	The Science Lab is open <br />';
	}
else
	{
	print '
	The Science Lab is closed <br />';
	}

// Science Library

if ((($post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Wednesday' || $post_day == 'Thursday') && $post_hour >= 8 && $post_hour <= 23) ||
     ($post_day == 'Friday' && $post_hour >= 8 && $post_hour < 5) ||
     ($post_day == 'Saturday' && $post_hour >= 1 && $post_hour < 5) ||
     ($post_day == 'Sunday' && $post_hour >= 1 && $post_hour <= 23))
	{
	print '
	The Science Library is open <br />';
	}
else
	{
	print '
	The Science Library is closed <br />';
	}
	
	
// MathLAN

if ((($post_day == 'Monday' || $post_day == 'Tuesday') && $post_hour >= 8 && $post_hour < 22) || (($post_day == 'Wednesday' || $post_day == 'Thursday') && $post_hour >= 8 && $post_hour <= 23) ||
    (($post_day == 'Monday' || $post_day == 'Thurday' || $post_day == 'Friday') && $post_hour == 0) ||
    ($post_day == 'Saturday' && $post_hour >= 12 && $post_hour < 6) ||
    ($post_day == 'Sunday' && $post_hour >= 12 && $post_hour <= 23))
	{
	print '
	The MathLAN is open <br />';
	}
else
	{
	print '
	The MathLAN is closed <br />';
	}

// IIF

if ((($post_day == 'Sunday' || $post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Wednesday' || $post_day == 'Thursday') && $post_hour >= 17 && $post_hour <= 23) ||
    (($post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Wednesday' || $post_day == 'Thursday' || $post_day == 'Friday') && $post_hour == 0) ||
     ($post_day == 'Friday' && $post_hour >= 17 && $post_hour < 22))
	{
	print '
	The IIF is open <br />';
	}
else
	{
	print '
	The IIF is closed <br />';
	}

// Burling

if ((($post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Wednesday' || $post_day == 'Thursday') && $post_hour >= 8 && $post_hour <= 23) || (($post_day == 'Monday' || $post_day == 'Tuesday' || $post_day == 'Wednesday' || $post_day == 'Thursday' || $post_day == 'Friday') && $post_hour == 0) ||
     ($post_day == 'Friday' && $post_hour >= 8 && $post_hour < 22) ||
     ($post_day == 'Saturday' && $post_hour >= 10 && $post_hour < 22) ||
     ($post_day == 'Sunday' && $post_hour >= 10 && $post_hour <= 23))
	{
	print '
	Burling Library is open <br />';
	}
else 
	{
	print '
	Burling Library is closed <br />';
	}
// Lazier, Read, Cleveland, Younker, Noyce 2402/2403 and Norris are always open
	print '
	Lazier Lab is open <br />
	Read Lab is open <br />
	Cleveland Lab is open <br />
	Younker Lab is open <br />
	Noyce 2402 and 2403 are open <br />
	Norris Lab is open</p>';

print '</div> <!-- End image div -->';

// software.php -- takes user input and parses it using the levenshtein algorithm

if (isset($_GET['software_id'])) {
	$software = $_GET['software_id'];
}

else {
	$software = $_POST['software_id'];
}

// Input misspelled word
$input = "$software";

// Array of words to check against (we should be able to update this list through KBOX, or by downloading software page and parsing for software
$words  = array('Photoshop CS3', 'Blender', 'Emacs', 'Google Earth', 'Google Docs', 'ChemDraw');

// No shortest distance found, yet
$shortest = -1;

// Loop through words to find the closest
foreach ($words as $word) {

    	// Calculate the distance between the input word and the current word
	$lev = levenshtein($input, $word);

    	// Check for an exact match
    	if ($lev == 0) {

        	// Closest word is this one (exact match)
        	$closest = $word;
        	$shortest = 0;

        	// Break out of the loop; we've found an exact match
        	break;
    	}

    	// If this distance is less than the next found shortest distance, OR if a next shortest word has not yet been found
    	if ($lev <= $shortest || $shortest < 0) {

        	// Set the closest match, and shortest distance
        	$closest  = $word;
        	$shortest = $lev;
    	}
}

echo "<p>Input word: $input<br />\n";

if ($shortest == 0) {
    	echo "Exact match found: $closest</p>\n";
} 

else {
    	echo "Did you mean <a href=\"checklabs.php?software_id=$closest&day=$post_day&hour=$post_hour\">$closest</a>?<br />\n";
}

// If the algorithm fails to find the correct piece of software, the user can try again or check out the list

if (!(isset($_GET['software_id']))) {
	echo "If not, you can <a href=\"index.php?tab_select=3\">try again</a> or check out the <a href=\"http://www.grinnell.edu/its/labsoft/\">list of available software</a></p>\n";
	}

print '
</body>
</html>';

?>
